package org.example.jedis;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.util.*;

/**
 * @author PC
 * jedis 测试demo
 */
public class JedisDemoTest {
    private static final Logger logger = LoggerFactory.getLogger(JedisDemoTest.class);

    public static void main(String[] args) {
        //创建Jedis对象
        Jedis jedis = new Jedis("*", 6379);
        //测试连接
        String value = jedis.ping("hello");
        System.out.println(value);
        //关闭连接
        jedis.close();
    }

    @Test
    public void testString() {
        //创建Jedis对象
        Jedis jedis = new Jedis("*", 6379);
        //添加数据
        jedis.set("str_key_1", "hello");
        jedis.set("str_key_2", "world");
        //获取数据
        logger.info(jedis.get("str_key_1"));
        logger.info(jedis.get("str_key_2"));
        //添加多个值
        jedis.mset("str_key_3", "tom", "str_key_4", "jerry");
        //获取多个值
        Optional.ofNullable(jedis.mget("str_key_1", "str_key_2", "str_key_3", "str_key_4"))
                .orElse(new ArrayList<>()).forEach(logger::info);
        //查找key
        Set<String> keySet = jedis.keys("*");
        keySet.forEach(logger::info);
        //关闭连接
        jedis.close();
    }

    @Test
    public void testList() {
        //创建Jedis对象
        Jedis jedis = new Jedis("*", 6379);
        //添加数据
        jedis.lpush("l_key_1", "tom", "and", "jerry");
        //获取数据
        Optional.ofNullable(jedis.lrange("l_key_1", 0, -1))
                .orElse(new ArrayList<>()).forEach(logger::info);
        //关闭连接
        jedis.close();
    }

    @Test
    public void testSet() {
        //创建Jedis对象
        Jedis jedis = new Jedis("*", 6379);
        //添加数据
        jedis.sadd("s_key_1", "tom", "and", "jerry", "and");
        //获取数据
        Optional.ofNullable(jedis.smembers("s_key_1"))
                .orElse(new HashSet<>()).forEach(logger::info);
        //关闭连接
        jedis.close();
    }

    @Test
    public void testHash() {
        //创建Jedis对象
        Jedis jedis = new Jedis("*", 6379);
        //添加数据
        jedis.hset("h_key_1", "name", "tom");
        //获取数据
        logger.info(jedis.hget("h_key_1", "name"));
        //通过map添加数据
        Map<String, String> map = new HashMap<>(2);
        map.put("name", "jerry");
        map.put("age", "21");
        jedis.hmset("h_key_2", map);
        //获取数据
        Optional.ofNullable(jedis.hmget("h_key_2", "name", "age"))
                .orElse(new ArrayList<>()).forEach(logger::info);
        //关闭连接
        jedis.close();
    }

    @Test
    public void testZSet() {
        //创建Jedis对象
        Jedis jedis = new Jedis("*", 6379);
        //添加数据
        jedis.zadd("z_key_1", 33.0, "math");
        Map<String, Double> map = new HashMap<>(2);
        map.put("english", 22.0);
        map.put("history", 44.0);
        jedis.zadd("z_key_1", map);
        //获取数据
        Optional.ofNullable(jedis.zrange("z_key_1", 0, -1))
                .orElse(new HashSet<>()).forEach(logger::info);
        //关闭连接
        jedis.close();
    }
}
